
package com.xin.di.uav.po;

import com.xin.di.uav.user.order.po.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    @Insert({"<script>",
            "insert into product (id, name, main_image, sale_price, supply_price, stock, `desc`, merchant_id,\n" +
                    "                     status, sort, rule_type, rule_info)\n" +
                    "values (#{id}, #{name}, #{mainImage}, #{salePrice}, #{supplyPrice}, #{stock}, #{desc}, #{merchantId},#{status}, #{sort}, #{ruleType}, #{ruleInfo})",
            "</script>"})
    int insert(Product product);

    int deleteById(String id);

    // 动态字段更新（只更新非空字段）
    @Update({
            "<script>",
            "UPDATE product ",
            "<set>",
            "    <if test='status != null'>status = #{status},</if>",
            "    <if test='name != null'>name = #{name},</if>",
            "    <if test='mainImage != null'>main_image = #{mainImage},</if>",
            "    <if test='salePrice != null'>sale_price = #{salePrice},</if>",
            "    <if test='supplyPrice != null'>supply_price = #{supplyPrice},</if>",
            "    <if test='stock != null'>stock = #{stock},</if>",
            "    <if test='merchantId != null'>merchant_id = #{merchantId},</if>",
            "    <if test='ruleType != null'>rule_type = #{ruleType},</if>",

            "    <if test='desc != null'>`desc`  = #{desc},</if>",

            "    <if test='status != null'>status = #{status},</if>",


            "    <if test='status != null'>status = #{status},</if>",
            "    updated_at = NOW()",

            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateById(Product product);

    @Select({"<script>",
            "select id, name, main_image, sale_price, supply_price, stock, \"desc\", merchant_id, created_at, updated_at,\n" +
                    "                     status, sort, rule_type, rule_info \n" +
                    "from product where id =#{id} ",
            "</script>"})
    Product selectById(String id);


    @Select({"<script>",
            "select count(1) \n" +
                    "from product where 1=1 ",
            "    <if test='name != null and name !=\"\" '> and name like CONCAT('%', #{name}, '%')</if>",
            "</script>"})
    int count(@Param("name") String name);


    @Select({"<script>",
            "select id, name, main_image,`desc`, sale_price, supply_price, rule_type,stock, merchant_id, created_at, updated_at, status \n" +
                    " from product where 1=1 " +
                    "    <if test='name != null and name !=\"\" '> and name like CONCAT('%', #{name}, '%')</if>",
            " order by created_at desc   limit #{offset},#{pageSize} ",
            "</script>"})
    List<Product> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize, @Param("name") String name);


    @Select({"<script>",
            "select id, name, main_image, sale_price, supply_price, stock, \"desc\", merchant_id, created_at, updated_at,\n" +
                    "                     status, sort, rule_type, rule_info from product  WHERE  merchant_id IN ",
            "       <foreach item=\"id\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">\n" +
                    "            #{id}\n" +
                    "        </foreach>" +
                    "</script>"})
    List<Product> selectByMerchantIds(@Param("ids") List<String> ids);
}
